package cn.doit.day04

import scala.collection.mutable

object _02_算子的开始 {
  def main(args: Array[String]): Unit = {

    //函数的至简原则
    val arr4: Array[Int] = Array(1, 2, 3, 4, 5)
    arr4.map((a:Int)=>{a*10})
    //如果，只有一个参数，()可以省略  并且类型也可以省略
    arr4.map(a=>{a*10})
    //如果函数体中只有一行，那么大括号可以省略
    arr4.map(a=> a*10)
    //如果参数前面和后面只用了一次，那么可以用_代替=》
    arr4.map(_*10)



    val arr = Array("xiang,ge,zhen,shuai","er,xiang,ye,shuai","xiang,ge,qing,bu,yao,chi,dao")

    // val arr = Array("xiang,ge,zhen,shuai","er,xiang,ye,shuai","xiang,ge,qing,bu,yao,chi,dao")
    //想用map来实现 返回给我Array("xiang","ge","zhen","shuai"...)  不能够实现的
    val array2: Array[Array[String]] = arr.map((a: String) => {
       a.split(",")
    })






    val flatten3: Array[String] = array2.flatten
    println(flatten3.mkString(","))

    //flatten 和  map的结合体  flatMap
    val res3: Array[String] = arr.flatMap((line: String) => {
      line.split(",")
    })
    println(res3.mkString(","))



    //将arr里面每一个元素都*10 最后再返回一个数组给我
    /*val ints: Array[Int] = arr.clone()
    var a: Int = 0
    for (elem <- arr) {
      ints.update(a, elem * 10)
      a += 1
    }
    println(ints.mkString(","))*/

//数组调用map ==》 返回值类型就是一个数组   进去：Array[Int] ==> 出来Array[类型]

    //map算子  将arr里面的每一个元素都赋值给对象中id的属性，然后将Xiang这个对象返回回来
    val res :Array[mutable.HashMap[String,Int]]= arr.map((line: String) => {
      val map: mutable.HashMap[String, Int] = new mutable.HashMap[String, Int]()
      val arr1: Array[String] = line.split(",")
      //将这一个个的单词都转换成(单词，1)形式返回给我
      for (elem <- arr1) {
        map.put(elem, 1)
      }
      map
    })





    //flatten   Array('h','e','l','l','o')
    val arr1: Array[String] = Array("hello", "hi", "xiangge")
    val flatten: Array[Char] = arr1.flatten
    println(flatten.mkString(","))//h,e,l,l,o,h,i,x,i,a,n,g,g,e


    //只能压一层
    val array: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
    val flatten1: Array[Int] = array.flatten//Array(1,2,3,4,5,6)
    println(flatten1.mkString(","))//1,2,3,4,5,6


    val array1: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3), Array(4, 5, 6)), Array(Array(7, 8, 9), Array(10, 11, 16)))
    val flatten2: Array[Array[Int]] = array1.flatten
    println(flatten2.map((a: Array[Int]) => {
      a.mkString(",")
    }).mkString("_"))








  }

}

case class Xiang(id:Int)
